<template>
  <div class="import-container">
    {{ $route.query.type }}
    <upload-excel :on-success="handleSuccess" :before-upload="beforeUpload" />
  </div>
</template>

<script>
import { reqUploadEmployee } from '@/api/employees'
export default {
  name: 'Import',
  computed: {
    type() {
      return this.$route.query.type
    }
  },
  methods: {
    // 上传之前 如果传了就要return true 才会通过校验
    beforeUpload(file) {
      const is1m = file.size / 1024 / 1024 / 1
      if (is1m) {
        return true
      }
      this.$message({
        message: '请上传1m大小的文件',
        type: 'warning'
      })
      return false
    },
    // 成功之后
    async handleSuccess({ results, header }) {
      if (this.type === 'user') {
        const userRelations = {
          '入职日期': 'timeOfEntry',
          '手机号': 'mobile',
          '姓名': 'username',
          '转正日期': 'correctionTime',
          '工号': 'workNumber',
          '聘用形式': 'formOfEmployment',
          '部门': 'departmentName'
        }
        // map 方法
        // const resarr = results.map(item => {
        //   const obj = {}
        //   for (const k in item) {
        //     const englishKey = userRelations[k]
        //     if (['timeOfEntry', 'correctionTime'].includes(englishKey)) {
        //       obj[englishKey] = this.formatExcelDate(item[k], '-')
        //     } else {
        //       obj[englishKey] = item[k]
        //     }
        //   // const englishKey = userRelations[k]
        //   // obj[englishKey] = item[k]
        //   }
        //   return obj
        // })
        // const { message } = await reqUploadEmployee(resarr)
        // this.$message.success(message)
        // this.$router.back()
        const arr = []
        results.forEach(item => {
          const obj = {}
          for (const k in item) {
            // console.log(item[k])
            console.log(userRelations[k])
            const englishKey = userRelations[k]
            obj[englishKey] = item[k]
            if (item[k] === '正式') {
              obj[englishKey] = 1
            } else if (item[k] === '非正式') {
              obj[englishKey] = 2
            }
            if (['timeOfEntry', 'correctionTime'].includes(englishKey)) {
              obj[englishKey] = this.formatExcelDate(item[k], '-')
            }
          }
          arr.push(obj)
        })
        const { message } = await reqUploadEmployee(arr)
        this.$message.success(message)
        this.$router.back()
      }
      if (this.type === 'social') {
        // 上传社保
      }
      if (this.type === 'social') {
      // 上传工资
      }
    },
    // excel读取2020/04/03 14:01这种时间格式是会将它装换成数字类似于3924.584027778 numb是传过来的整数数字，format是之间间隔的符号
    formatExcelDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date)
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
    // console.log(formatDate(3924.584027778, '-')) // 2020-04-03 14:01
  }

}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
